package com.jss.lanouhn.fristapp.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.adapters.BaseSecondAdapter;
import com.jss.lanouhn.fristapp.modles.BaseSecondUse;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/8/27.
 */
public class SuperActivity extends AppCompatActivity {
    private TextView tv_title;
    private RecyclerView recyclerView;
    private String url;
    private BaseSecondUse infos;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_base);
        initView();
        initData();
    }

    private void initView() {
        tv_title = (TextView) findViewById(R.id.tv_base_center);
        recyclerView = (RecyclerView) findViewById(R.id.rv_home_base);
        Bundle bundle=this.getIntent().getExtras();
        int id=bundle.getInt("id");
        url = "http://api.liwushuo.com/v2/collections/" + id + "/posts?gender=1&generation=2&limit=20&offset=0";
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        infos = gson.fromJson(result, BaseSecondUse.class);
                        if (infos.getData().getPosts().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                });
            }
        }).start();
    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    tv_title.setText(infos.getData().getTitle());
                    LinearLayoutManager manager = new LinearLayoutManager(SuperActivity.this);
                    recyclerView.setLayoutManager(manager);
                    BaseSecondAdapter adapter = new BaseSecondAdapter(infos, SuperActivity.this);
                    recyclerView.setAdapter(adapter);
                    break;
                case 0:
                    Toast.makeText(SuperActivity.this, "解析失败", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}
